package com.hzlh.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hzlh.common.util.BaseConstants;
import com.hzlh.core.utils.StringUtils;
import com.hzlh.domain.po.PermissionsPo;
import com.hzlh.domain.po.RolePermPo;
import com.hzlh.domain.po.RolePo;
import com.hzlh.domain.po.UserRolesPo;
import com.hzlh.mapper.RolePermMapper;

@Service
public class RolePermService {

	@Autowired
	private RolePermMapper rolePermMapper;
	
	public List<String> getRoleAuthorizNames(Long roleId){
		return rolePermMapper.getRoleAuthorizNames(roleId);
	}
	
	public List<PermissionsPo> getPermAuthorizName(String authorizname){
		return rolePermMapper.getPermAuthorizName(authorizname);
	}
	
	public Map<String,Object> permPage(String permLevel,String permName,Long parentId,int pageIndex,int pageSize){
		Map<String, Object> remap = new HashMap<String,Object>();
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("start", pageIndex);
		map.put("end", pageSize);
		if(StringUtils.isNotBlank(permLevel)){
			map.put("permLevel", permLevel);
		}
		if(StringUtils.isNotBlank(permName)){
			map.put("permName", permName);
		}
		//全部 -1 
		if(parentId != null && parentId != -1L){
			map.put("parentId", parentId);
		}
		int count = rolePermMapper.getPermissionsCount(map);
		List<PermissionsPo> list = null;
		if(count>0){
			list = rolePermMapper.getPermissionsPage(map);
		}
		remap.put(BaseConstants.MCPP_HC_TOTAL_RECORDS, count);
		remap.put(BaseConstants.MCPP_HC_PAGE_DATA, list);
		return remap;
	}
	
	public List<PermissionsPo> permTreeList(){
		return rolePermMapper.getParentPerms();
	}
	
	public List<PermissionsPo> getAllPermissions(Map<String , Object> map){
		return rolePermMapper.getAllPermissions(map);
	}
	
	public int addPerm(PermissionsPo po){
		return rolePermMapper.insertPermissions(po);
	}
	
	public int editPerm(PermissionsPo po){
		return rolePermMapper.updatePermissions(po);
	}
	
	public PermissionsPo getPerm(Long id){
		return rolePermMapper.getPermissionsById(id);
	}
	
	public boolean deletePerm(Long[] ids){
		return rolePermMapper.deletePermissions(ids) > 0;
	}
	
	public Map<String,Object> rolePage(int weight,int pageIndex,int pageSize){
		Map<String, Object> remap = new HashMap<String,Object>();
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("weight",weight);
		map.put("start", pageIndex);
		map.put("end", pageSize);
		int count = rolePermMapper.getRoleCount(map);
		List<RolePo> list = null;
		if(count>0){
			list = rolePermMapper.getRolePage(map);
		}
		remap.put(BaseConstants.MCPP_HC_TOTAL_RECORDS, count);
		remap.put(BaseConstants.MCPP_HC_PAGE_DATA, list);
		return remap;
	}
	
	public List<RolePo> roleList(Map<String,Object> map){
		return rolePermMapper.getRoleList(map);
	}
	
	public List<RolePo> getUserRole(Long userId){
		return rolePermMapper.getUserRole(userId);
	}
	
	public int addRole(RolePo po){
		return rolePermMapper.insertRole(po);
	}
	
	public int editRole(RolePo po){
		return rolePermMapper.updateRole(po);
	}
	
	public RolePo getRole(Long id){
		return rolePermMapper.getRoleById(id);
	}
	
	public boolean deleteRole(Long[] id){
		return rolePermMapper.deleteRole(id) > 0;
	}
	
	public int batchAddUserRole(List<UserRolesPo> pos){
		return rolePermMapper.batchInsertUserRole(pos);
	}
	
	public boolean deleteUserRole(Long[] userIds){
		return rolePermMapper.deleteUserRole(userIds);
	}

	public int updatePermSort(List<PermissionsPo> list) {
		return rolePermMapper.updatePermSort(list);
	}

	public Integer getRankNum(Long parentId) {
		return rolePermMapper.getRankNum(parentId) + 1;
	}
	
	public int batchAddRolePerm(List<RolePermPo> pos){
		return rolePermMapper.batchAddRolePerm(pos);
	}

	public boolean deleteRolePerm(Long[] roleId) {
		return rolePermMapper.deleteRolePerm(roleId) > 0;
	}

	public boolean deleteRolePermIds(Long[] permissionId) {
		return rolePermMapper.deleteRolePermIds(permissionId) > 0;
	}

	public List<Long> getRolePermIds(Long roleId) {
		return rolePermMapper.getRolePermIds(roleId);
	}
}